package com.me.CoaB;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;

public class MainMenuScreen implements Screen {
	
	final CoaB game;
	
	// Sky
	private Texture skySplash;
	private TextureRegion skyRegion;
	private Sprite skySprite;
	
	// Grass
	private Texture grassSplash;
	private Sprite grassSprite;
	
	// Logo
	private Texture logoTextPink;
	private Texture logoTextYellow;
	private Sprite logoSprite;
	private int flashLogo = 0;

	private static final float BUTTON_WIDTH = 300f;
	private static final float BUTTON_HEIGHT = 60f;
	private static final float BUTTON_SPACING = 10f;
	
	private Music rainMusic;
	
	
	float stateTime;
	
	
	//private WorldRenderer renderer;
	OrthographicCamera camera;
	private World world;
	ShapeRenderer debugRenderer = new ShapeRenderer();
	// mainchar's world
		
	public MainMenuScreen(final CoaB gam) {
		game = gam;
		
		rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
		rainMusic.setLooping(true);
		//logo stuff
		
		logoTextYellow = new Texture(Gdx.files.internal("logoyellow.png"));
		logoTextPink = new Texture(Gdx.files.internal("logopink.png"));
		
		stateTime = 0f;  
		
		
	}
	
	public void create() {
				
	}
	
	
	@Override
	public void render (float delta) {
		Gdx.gl.glClearColor(0, 0, 0.2f, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
		
		this.camera = new OrthographicCamera();
		this.camera.setToOrtho(false, 800, 480);
		this.camera.update();
		
		
		stateTime += Gdx.graphics.getDeltaTime();
		
		MainCharacter mainchar = world.getMainCharacter();
		mainchar.UpdatePlayer(stateTime);
		game.batch.setProjectionMatrix(camera.combined);
		game.batch.begin();	
		
		skySprite.draw(game.batch);
		grassSprite.draw(game.batch);
		
		game.batch.draw(mainchar.currentFrame, mainchar.position.x, mainchar.position.y);
		
		game.font.draw(game.batch, "Click anywhere to start", 500, 100);
		
		// let's make the logo flash
		if (flashLogo < 20) {
			logoSprite = new Sprite(logoTextYellow);
			flashLogo++;
		} else if (flashLogo < 40) {
			logoSprite = new Sprite(logoTextPink);
			flashLogo++;
			if (flashLogo >= 40) flashLogo = 0;
		}
		logoSprite.setSize(399,206);
		logoSprite.setPosition(400,250);
		logoSprite.draw(game.batch);
		
		game.batch.end();
		
		if (Gdx.input.isTouched()) {
			game.setScreen(new GameScreen(game));
			rainMusic.stop();
			dispose();
		}
		
	}
	@Override
	public void resize(int width, int height) {
		
		
	}

	@Override
	public void show() {
		world = new World();
		
		skySplash = new Texture(Gdx.files.internal("sky.png"));
		skySprite = new Sprite(skySplash);
		skySprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		
		grassSplash = new Texture(Gdx.files.internal("grass.png"));
		grassSprite = new Sprite(grassSplash);
		grassSprite.setSize(800, 150);
		grassSprite.setPosition(0, 0);
		
		rainMusic.play();
		
	}

	@Override
	public void hide() {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
		
		skySplash.dispose();
		grassSplash.dispose();
	}
	
}
